﻿using System;
using System.Diagnostics;
using System.Reflection;
using System.Security;
using System.Text;

#if SILVERIGHT 
using System.Windows;
#endif

namespace OpenLightGroup.Common.ApplicationServices
{
    public class ClientApplicationInformation
    {
        internal static string GetStackInformation()
        {
            var detailsBuilder = new StringBuilder();

            var trace = new StackTrace();

            var frames = trace.GetFrames();

            Assembly assem = Assembly.GetCallingAssembly();

            detailsBuilder.AppendLine(String.Empty);

            detailsBuilder.AppendLine("Stack Information:");

            detailsBuilder.AppendLine(String.Empty);

            foreach (var frame in frames)
            {
                //detailsBuilder.AppendFormat("File: {0}",
                //                   frame.GetFileName());
                detailsBuilder.AppendLine(String.Empty);
                detailsBuilder.AppendFormat("Method: {0}",
                                            frame.GetMethod());

                detailsBuilder.AppendLine(String.Empty);

#if  !WINDOWS_PHONE

                detailsBuilder.AppendFormat("Line: {0}",
                                            frame.GetFileLineNumber());
#endif
            }

            return detailsBuilder.ToString();
        }

        //ToDo refactor this to be platform specific
        [SecuritySafeCritical]
        public static string GetDetails()
        {
            var detailsBuilder = new StringBuilder();

#if !WINDOWS_PHONE

#if SILVERLIGHT

            detailsBuilder.AppendLine(String.Empty);
            detailsBuilder.AppendLine("Silverlight Application:");

            detailsBuilder.AppendFormat("Out-of-Browser: {0}",
                                        System.Windows.Application.Current.IsRunningOutOfBrowser);

            detailsBuilder.AppendLine(String.Empty);

            detailsBuilder.AppendFormat("Has Elevated Permissions: {0}",
                                        System.Windows.Application.Current.HasElevatedPermissions);

            detailsBuilder.AppendLine(String.Empty);

            detailsBuilder.AppendFormat("Install State: {0}",
                                        System.Windows.Application.Current.InstallState.ToString());

            detailsBuilder.AppendLine(String.Empty);


            detailsBuilder.AppendLine(string.Format("Root Visual: {0}", System.Windows.Application.Current.RootVisual.ToString()));

            detailsBuilder.AppendLine(string.Format("Navigation State: {0}", System.Windows.Application.Current.Host.NavigationState));

            detailsBuilder.AppendLine(string.Format("Local Path: {0}", System.Windows.Application.Current.Host.Source.LocalPath));

            detailsBuilder.AppendLine(string.Format("Absolute Path: {0}", System.Windows.Application.Current.Host.Source.AbsoluteUri));

            detailsBuilder.AppendLine(string.Format("Host: {0}", System.Windows.Application.Current.Host.Source.Host));

#else

            detailsBuilder.AppendLine("Desktop Application:");

            if (AppDomain.CurrentDomain.ApplicationIdentity != null)
            {

            detailsBuilder.AppendFormat("Application Name: {0}", AppDomain.CurrentDomain.ApplicationIdentity.FullName);
            detailsBuilder.AppendLine(String.Empty);

            }

            detailsBuilder.AppendFormat("Base Directory: {0}", AppDomain.CurrentDomain.BaseDirectory);
            detailsBuilder.AppendLine(String.Empty);

#endif

#else
            detailsBuilder.AppendLine("Windows Phone Application");
            
#endif

            return detailsBuilder.ToString();
        }
    }
}